/*
 * Log.cpp
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <android/log.h>
#include "app_sys_log.h"
#define __LOG_TAG__ "L1-"

void app_sys_info(const char *pMessage, ...)
{
	va_list lVarArgs;

	va_start(lVarArgs, pMessage);
	__android_log_vprint(ANDROID_LOG_INFO, __LOG_TAG__, pMessage, lVarArgs);
	__android_log_print(ANDROID_LOG_INFO, __LOG_TAG__, "");
	va_end(lVarArgs);
}

void app_sys_error(const char *pMessage, ...)
{
	va_list lVarArgs;

	va_start(lVarArgs, pMessage);
	__android_log_vprint(ANDROID_LOG_ERROR, __LOG_TAG__, pMessage, lVarArgs);
	__android_log_print(ANDROID_LOG_ERROR, __LOG_TAG__, "");
	va_end(lVarArgs);
}
void app_sys_warn(const char *pMessage, ...)
{
	va_list lVarArgs;

	va_start(lVarArgs, pMessage);
	__android_log_vprint(ANDROID_LOG_WARN, __LOG_TAG__, pMessage, lVarArgs);
	__android_log_print(ANDROID_LOG_WARN, __LOG_TAG__, "");
	va_end(lVarArgs);
}
void app_sys_debug(const char *pMessage, ...)
{
	va_list lVarArgs;
	
	va_start(lVarArgs, pMessage);
	__android_log_vprint(ANDROID_LOG_DEBUG, __LOG_TAG__, pMessage, lVarArgs);
	__android_log_print(ANDROID_LOG_DEBUG, __LOG_TAG__, "");
	va_end(lVarArgs);
}

static void dump_max_data(unsigned char *frame, unsigned int length)
{
	unsigned int bufLength = 3 * length + 1;
	unsigned char *buf = (unsigned char *)malloc(bufLength * sizeof(unsigned char));
	memset(buf, 0, bufLength);
	unsigned int i;
	for (i = 0; i < length; i++)
	{
		sprintf(buf + i * 3, "%02x ", frame[i]);
	}
	app_sys_debug("%s", buf);
	free(buf);
}

static void dump_frame_data(unsigned char *frame, unsigned int length)
{
	int start = 0;
	int pln = length;
	int max = 300;
	int dump_len = 0;

	while (start < length)
	{
		dump_len = pln > max ? max : pln;
		dump_max_data(frame + start, dump_len);
		pln -= dump_len;
		start += dump_len;
	}
}

void app_sys_dump(const char *str, unsigned char *pBuf, unsigned int len)
{
	unsigned int i, j, nRemain;
	char tmp[17];
	char *p;
	char *add = pBuf;
	unsigned int nLine = 0;

	char strLine[32];

	if (str != NULL)
		app_sys_info("%s: length = %d [0x%x]", str, len, pBuf);

	if (len == 0 || pBuf == NULL)
		return;

	dump_frame_data(pBuf, len);

	return;
}
